fac = {'0': 1}
res = 1
for i in range(1, 10):
    res = res * i
    fac[str(i)] = res

nums_time = {2: 1,1: 1,145: 1, 169: 3, 363601: 3, 1454: 3, 871: 2, 45361: 2, 872: 2, 45362: 2}
endres = 0
for i in range(3, 10 ** 6):
    tmp = i
    link_nums = 1
    tmps_link = [i]
    while True:
        sums = 0
        for j in str(tmp):
            sums = sums + fac[j]
        if sums == tmp:
            if tmp not in nums_time:
                nums_time[tmp] = 1
            link_nums += 1
            nums_time[i] = link_nums
            break
        else:
            if sums in nums_time:
                link_nums = link_nums + nums_time[sums]
                nums_time[i] = link_nums
                print("数字%d的回路长度是：%d" % (i, link_nums))
                if link_nums==60:
                    endres += 1
                break
            else:
                link_nums += 1
                tmp = sums

print(endres)